﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SmartSapce.ServiceLocation;
using SmartSpace.Core;
using SmartSpace.Web;
using SmartSpace.Workflow;

namespace SmartSpace.WebApp.Modules.Workflow.Pages
{
    public partial class Workflow_Drive : System.Web.UI.Page
    {
        public string RefModule { get; private set; }
        public Guid RefId { get; private set; }
        public Guid EventId { get; private set; }
        public EventMetadata Event { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            RefModule = Request.GetString("RefModule");
            RefId = Request.GetGuid("RefId");
            EventId = Request.GetGuid("EventId");

            if (!IsPostBack)
            {
                // 获取用户
                ICoreService coreService = ServiceLocator.Get<ICoreService>();
                PagedList<User> users = coreService.GetUsers(0, 999);

                this._ctlList.DataSource = users;
                this._ctlList.DataBind();
            }

            IWorkflowService wfService = ServiceLocator.Get<IWorkflowService>();
            Event = wfService.GetEvent(EventId).Data;
            if (Event.IsNeedPickUser)
            {
                _ctlPanelPickUser.Visible = true;
            }
            else
            {
                _ctlPanelPickUser.Visible = false;
            }
            if (Event.IsNeedInputComment)
            {
                _ctlPanelComment.Visible = true;
            }
            else
            {
                _ctlPanelComment.Visible = false;
            }
            _ctlTxtEvent.InnerText = Event.Caption;
        }

        protected void _ctlBtnSubmit_Click(object sender, EventArgs e)
        {
            if (Event.IsNeedPickUser)
            {
                string receiverAccount = string.Empty;
                string receiverName = string.Empty;

                foreach (RepeaterItem item in _ctlList.Items)
                {
                    CheckBox checkBox = (CheckBox)item.FindControl("_ctlCheck");
                    if (checkBox != null && checkBox.Checked)
                    {
                        HiddenField hiddenValue = (HiddenField)item.FindControl("_ctlHiddenValue");
                        HiddenField hiddenName = (HiddenField)item.FindControl("_ctlHiddenName");

                        receiverAccount = hiddenValue.Value;
                        receiverName = hiddenName.Value;
                        break;
                    }
                }

                if (!string.IsNullOrEmpty(receiverAccount) && !string.IsNullOrEmpty(receiverName))
                {
                    IWorkflowService wfService = ServiceLocator.Get<IWorkflowService>();
                    WorkflowInstance instance = wfService.GetInstance(RefModule, RefId).Data;
                    List<string> receivers = new List<string>();
                    receivers.Add(receiverAccount);
                    wfService.DriveInstance(instance.Id, EventId, RefId, RefModule, User.Identity.Name, receivers);
                    Response.Redirect("./Workflow-View.aspx?refModule=" + RefModule + "&refId=" + RefId.ToString());
                }
            }
            else
            {
                IWorkflowService wfService = ServiceLocator.Get<IWorkflowService>();
                WorkflowInstance instance = wfService.GetInstance(RefModule, RefId).Data;
                List<string> receivers = new List<string>();
                receivers.Add(instance.Creator);
                wfService.DriveInstance(instance.Id, EventId, RefId, RefModule, User.Identity.Name, receivers);
                Response.Redirect("./Workflow-View.aspx?refModule=" + RefModule + "&refId=" + RefId.ToString());
            }
        }

        protected void _ctlBtnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("./Workflow-View.aspx?refModule=" + RefModule + "&refId=" + RefId.ToString());
        }
    }
}